package com.xinghai.web.controller.dingtalk;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinghai.common.annotation.Log;
import com.xinghai.common.core.controller.BaseController;
import com.xinghai.common.core.domain.AjaxResult;
import com.xinghai.common.core.domain.entity.SysDept;
import com.xinghai.common.core.page.TableDataInfo;
import com.xinghai.common.enums.BusinessType;
import com.xinghai.common.exception.ServiceException;
import com.xinghai.common.utils.StringUtils;
import com.xinghai.common.utils.poi.ExcelUtil;
import com.xinghai.system.domain.XhCarInfo;
import com.xinghai.system.domain.XhDriverInfo;
import com.xinghai.system.domain.vo.XhDriverInfoVo;
import com.xinghai.system.service.ISysDeptService;
import com.xinghai.system.service.IXhDriverInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * 司机信息Controller
 * 
 * @author smy
 * @date 2024-07-21
 */
@RestController
@RequestMapping("/driver/info")
public class XhDriverInfoController extends BaseController
{
    @Autowired
    private IXhDriverInfoService xhDriverInfoService;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询司机信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(XhDriverInfo xhDriverInfo)
    {
        startPage();
        List<XhDriverInfo> list = xhDriverInfoService.list(xhDriverInfo);
        return getDataTable(list);
    }

    /**
     * 导出司机信息列表
     */
    @Log(title = "司机信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XhDriverInfo xhDriverInfo)
    {
        List<XhDriverInfo> list = xhDriverInfoService.list(xhDriverInfo);
        ExcelUtil<XhDriverInfo> util = new ExcelUtil<XhDriverInfo>(XhDriverInfo.class);
        util.exportExcel(response, list, "司机信息数据");
    }

    /**
     * 获取司机信息详细信息
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo(Long id)
    {
        return success(xhDriverInfoService.getInfo(id));
    }

    /**
     * 新增司机信息
     */
    @Log(title = "司机信息", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public AjaxResult add(@RequestBody XhDriverInfo xhDriverInfo) throws Exception {
        return toAjax(xhDriverInfoService.insert(xhDriverInfo));
    }

    /**
     * 修改司机信息
     */
    @Log(title = "司机信息", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody XhDriverInfo xhDriverInfo) throws Exception {
        return toAjax(xhDriverInfoService.update(xhDriverInfo));
    }

    /**
     * 删除司机信息
     */
    @Log(title = "司机信息", businessType = BusinessType.DELETE)
	@PostMapping("/delete")
    public AjaxResult delete(Long id) throws Exception {
        return toAjax(xhDriverInfoService.delete(id));
    }


    /**
     * 批量删除司机信息
     */
    @Log(title = "司机信息", businessType = BusinessType.DELETE)
    @PostMapping("/deleteBatch")
    public AjaxResult deleteBatch(Long[] ids) throws Exception {
        return success(xhDriverInfoService.deleteBatch(ids));
    }

    /**
     * 导入司机信息模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<XhDriverInfo> util = new ExcelUtil<XhDriverInfo>(XhDriverInfo.class);
        util.importTemplateExcel(response, "司机信息数据模板");
    }


    /**
     * 导入司机信息
     */
    @Log(title = "司机信息管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        ExcelUtil<XhDriverInfo> util = new ExcelUtil<XhDriverInfo>(XhDriverInfo.class);
        List<XhDriverInfo> xhDriverInfoList = null;
        try {
            xhDriverInfoList = util.importExcel(file.getInputStream());
        } catch (IOException e) {
            throw new ServiceException("导入数据失败");
        }
        return success(xhDriverInfoService.importData(xhDriverInfoList));
    }

    /**
     * 车辆信息页面查询司机信息列表
     */
    @GetMapping("/car/driver/list")
    public AjaxResult carDriverList(XhDriverInfo xhDriverInfo)
    {
        List<XhDriverInfo> list = xhDriverInfoService.carDriverList(xhDriverInfo);
        return success(list);
    }

    /**
     * 接单页面-根据车辆ID查询司机列表
     */
    @GetMapping("/list/by/car")
    public AjaxResult getDriverListByCar(XhCarInfo xhCarInfo)
    {
        return success(xhDriverInfoService.getDriverListByCar(xhCarInfo));
    }
}
